package utils;

import io.XMLControler;

import java.awt.Color;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Vector;

import javax.swing.ImageIcon;

public class EDCUtils {
	
	public static File[] getFiles(String dirPath) {
        File f = new File(dirPath);
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
		        return (name.endsWith(".dtd"));
		    }
		};
		return f.listFiles(filter);
	}
	
	public static Vector<Object> getData(File file) {
		Vector<Object> v = new Vector<Object>();
		v.add(0, file.getName());
		v.add(1, ""); // link
		v.add(2, ""); // description
		v.add(3, ""); // comment
		return v;
	}
	
	public static Vector<Vector<Object>> getXMLData(String xmlfile) throws Exception {
		Vector<Vector<Object>> v = new Vector<Vector<Object>>();
		XMLControler.readXMLFile(xmlfile);
		v = XMLControler.getData();
		XMLControler.saveXMLFile(xmlfile);
		return v;
	}
	
	public static Color randomColor() {
        return new Color((int) (Math.random() * 256),  // Red
                         (int) (Math.random() * 256),  // Green
                         (int) (Math.random() * 256)); // Blue
    }
	
	/*
     * Get the extension of a file.
     */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = EDCUtils.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public static String getHeader(String fileName){
    	String res = "File name : "+fileName+"\n";
    	return res;
    }
    
    public static String getRender(String fileName){
    	String res="";
    	Vector emptyErrorsElements = XMLControler.getExceptions(fileName);
    	Vector anyErrorsElements = XMLControler.getAnyErrors(fileName);
    	Vector exceptionMessages = XMLControler.getExceptions(fileName);
    	
    	//Header
    	String title = getHeader(fileName);
    	String parsingTime = "Parsing time : "+XMLControler.getParsingTime(fileName)+"\n";
    	
    	//Empty error elements
    	String msgEmptyError ="";
    	if(emptyErrorsElements.size()==0){
    		msgEmptyError = "No EMPTY errors\n";
    	}
    	else{
    		msgEmptyError = "EMPTY errors for elements : \n";
    		for(int i=0; i<emptyErrorsElements.size();i++){
    			String emptyElt = (String)emptyErrorsElements.get(i);
    			msgEmptyError+= emptyElt+"\n";
    		}
    	}
    	
    	//ANY error elements
    	String msgAnyError ="";
    	if(anyErrorsElements.size()==0){
    		msgAnyError = "No ANY errors\n";
    	}
    	else{
    		msgAnyError = "ANY errors for elements : \n";
    		for(int i=0; i<anyErrorsElements.size();i++){
    			String anyElt = (String)anyErrorsElements.get(i);
    			msgAnyError+= anyElt+"\n";
    		}
    	}
    	
    	//Exception messages
    	String msgException ="";
    	if(exceptionMessages.size()==0){
    		msgException = "No exceptions\n";
    	}
    	else{
    		msgException = "Exception messages : \n";
    		for(int i=0; i<exceptionMessages.size();i++){
    			String msgElt = (String)exceptionMessages.get(i);
    			msgException+= msgElt+"\n";
    		}
    	}
    	return title+parsingTime+msgEmptyError+msgAnyError+msgException;
    }
    
}
